public class AddTwoNumbers {
    // leetcode 两数相加
    // https://leetcode.cn/problems/add-two-numbers/description/?envType=study-plan-v2&envId=top-100-liked
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        ListNode cur = head;
        // tmp记录进制
        int tmp = 0;
        while (l1 != null || l2 != null || tmp != 0) {
            int num1 = l1 == null ? 0 : l1.val;
            int num2 = l2 == null ? 0 : l2.val;
            ListNode newNode = new ListNode((num1 + num2 + tmp) % 10);
            tmp = (num1 + num2 + tmp) / 10;
            cur.next = newNode;
            cur = cur.next;
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
        }
        return head.next;
    }
}
